<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 服务管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-plus" class="handle-del mr10" @click="addGoodsType(1)">新增一级分类</el-button>
                <el-button type="primary" icon="el-icon-plus" class="handle-del mr10" @click="addGoodsType(2)">新增二级分类</el-button>
                <el-select v-model="query.typeId" placeholder="一级分类" class="handle-select mr10" @change="getServiceShopS">
                    <el-option v-for="item in goodsType" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                </el-select>
            </div>
            <el-table
                :data="goodsTypeS"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="secondMenu" label="分类名"></el-table-column>
                <el-table-column prop="typeName" label="一级分类"></el-table-column>
                <el-table-column label="分类图" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.img" :preview-src-list="[scope.row.thumb]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="一级分类">
                    <el-input v-model="form.typeName"></el-input>
                </el-form-item>
                <el-form-item label="二级分类">
                    <el-input v-model="form.secondMenu"></el-input>
                </el-form-item>
                <el-form-item label="二级分类图片">
                    <el-upload
                        class="upload-demo"
                        action="http://funvalue.top:23323/goodsmanage/upload"
                        :on-success="getUrl"
                        :file-list="fileList"
                        :headers="this.token"
                        :limit="1"
                        list-type="picture"
                    >
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="选择分类">
                    <el-select v-model="form.serviceTypeFk" placeholder="选择一级分类" class="handle-select mr10" @change="getServiceShopS">
                        <el-option v-for="item in goodsType" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增一级分类弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisibleT" width="30%">
            <el-form ref="addform" :model="addform" label-width="120px">
                <el-form-item label="一级分类名称">
                    <el-input v-model="addform.typeName"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="addGoodsTypes(1)">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增二级分类弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisibleTS" width="30%">
            <el-form ref="addform" :model="addform" label-width="120px">
                <el-form-item label="选择分类">
                    <el-select v-model="addform.tyid" placeholder="选择一级分类" class="handle-select mr10" @change="getServiceShopS">
                        <el-option v-for="item in goodsType" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="二级分类">
                    <el-input v-model="addform.typeName"></el-input>
                </el-form-item>
                <el-form-item label="二级分类图片">
                    <el-upload
                        class="upload-demo"
                        action="http://funvalue.top:23323/goodsmanage/upload"
                        :on-success="getUrlAdd"
                        :file-list="fileList"
                        :headers="this.token"
                        :limit="1"
                        list-type="picture"
                    >
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="addGoodsTypes(2)">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getGoodsTypes, getclassify, updateGoodsType, deleteGoodsTypeS, addGoodsType } from '../../api/goodsType';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                typeId: '',
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 100
            },
            addform: {},
            editVisibleTS: false,
            editVisibleT: false,
            token: { token: localStorage.getItem('token') },
            fileList: [],
            goodsType: [],
            goodsTypeS: [],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            editDescribeList: []
        };
    },
    created() {
        this.getServiceShop();
    },
    methods: {
        //图片上传模块
        getUrl(response, file, fileList) {
            this.form.imgsrc = response.data;
            console.log(this.form);
        },
        getUrlAdd(response, file, fileList) {
            this.addform.img = response.data;
            console.log(this.form);
        },
        //获取二级分类
        getServiceShopS() {
            getGoodsTypes(this.query).then((res) => {
                this.goodsTypeS = JSON.parse(JSON.stringify(res.data));
                this.pageTotal = res.data.length;
                for (let i = 0; i < this.goodsTypeS.length; i++) {
                    for (let j = 0; j < this.goodsType.length; j++) {
                        if (this.goodsTypeS[i].serviceTypeFk == this.goodsType[j].id) {
                            this.goodsTypeS[i].typeName = this.goodsType[j].typeName;
                        }
                    }
                }
            });
        },
        //获取分类
        getServiceShop() {
            getclassify().then((res) => {
                this.goodsType = JSON.parse(JSON.stringify(res.data));
                this.getServiceShopS();
            });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            fetchData(this.query).then((res) => {
                console.log(res);
                this.tableData = res.list;
                this.pageTotal = res.pageTotal || 50;
            });
        },
        //触发新增按钮
        addGoodsType(type) {
            this.addform = {};
            this.fileList = [];
            if (type == 1) {
                this.editVisibleT = true;
            } else if (type == 2) {
                this.editVisibleTS = true;
            }
        },
        //提交新增请求
        addGoodsTypes(type) {
            this.addform.type = type;
            addGoodsType(this.addform).then((res) => {
                if (res.code == 200) {
                    this.$message.success('新增成功');
                    this.editVisibleTS = this.editVisibleT = false;
                    this.getServiceShop();
                } else {
                    this.$message.error('新增失败，请联系管理员');
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    console.log(row);
                    deleteGoodsTypeS(row).then((res) => {
                        if (res.code == 200) {
                            this.$message.success('删除成功');
                            this.tableData.splice(index, 1);
                            this.getServiceShop();
                        } else {
                            this.$message.error('删除失败，请联系管理员');
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = JSON.parse(JSON.stringify(row));
            this.fileList = [{ url: row.img }];
            this.editVisible = true;
            console.log(this.form);
        },
        // 保存编辑
        saveEdit() {
            updateGoodsType(this.form).then((res) => {
                if (res.code == 200) {
                    this.editVisible = false;
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                    this.$set(this.tableData, this.idx, this.form);
                    this.getServiceShop();
                } else {
                    this.$message.error('修改失败，请联系管理员');
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
